<?php

/**
 * @var string $title 页面标题
 * @var string $action 表单提交地址
 * @var array{id?:int,title?:string,content?:string} $post
 */
?>
<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title><?php echo htmlspecialchars($title ?? '编辑', ENT_QUOTES, 'UTF-8'); ?></title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica, Arial;
            margin: 2rem;
            line-height: 1.6;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
        }

        label {
            display: block;
            margin: .6rem 0 .25rem;
        }

        input[type="text"],
        textarea {
            width: 100%;
            padding: .6rem;
            border: 1px solid rgba(127, 127, 127, .3);
            border-radius: 6px;
        }

        textarea {
            min-height: 220px;
            resize: vertical;
        }

        .actions {
            margin-top: 1rem;
        }

        .actions button,
        .actions a {
            margin-right: .5rem;
        }

        .auth {
            display: flex;
            gap: .5rem;
            align-items: center;
            margin-bottom: 1rem;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="auth">
            <?php $u = \App\Services\Auth::user();
            if ($u): ?>
                <span>已登录：<?php echo htmlspecialchars((string)$u['username'], ENT_QUOTES, 'UTF-8'); ?></span>
                <a href="/logout">退出</a>
            <?php else: ?>
                <a href="/login">登录</a>
                <a href="/register">注册</a>
            <?php endif; ?>
        </div>

        <h1><?php echo htmlspecialchars($title ?? '编辑', ENT_QUOTES, 'UTF-8'); ?></h1>
        <form action="<?php echo htmlspecialchars($action ?? '#', ENT_QUOTES, 'UTF-8'); ?>" method="post">
            <?php echo \App\Core\CSRF::input(); ?>

            <label for="title">标题</label>
            <input type="text" id="title" name="title" required value="<?php echo htmlspecialchars((string)($post['title'] ?? ''), ENT_QUOTES, 'UTF-8'); ?>">

            <label for="content">内容</label>
            <textarea id="content" name="content" required><?php echo htmlspecialchars((string)($post['content'] ?? ''), ENT_QUOTES, 'UTF-8'); ?></textarea>

            <div class="actions">
                <button type="submit">保存</button>
                <a href="/posts">返回列表</a>
            </div>
        </form>
    </div>
</body>

</html>